﻿using System;
using System.Collections.Generic;
using System.Text;
using SharpDiary.Core.Data;
using SharpDiary.Core.Utility;

namespace SharpDiary.Core.ORMapping
{
    /// <summary>
    /// 命令信息。
    /// </summary>
    [TableInfoAttribute("ActionInfo")]
    public class ActionInfo : DbMappingObjectBase
    {
        #region Fields & Properties

        private string _id = string.Empty;

        /// <summary>
        /// 获取或设置命令编号。
        /// </summary>
        [FieldMapping(true, System.Data.DbType.String, false, true)]
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _name = string.Empty;

        /// <summary>
        /// 获取或设置命令名称。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _description = string.Empty;

        /// <summary>
        /// 获取或设置详细描述信息。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        private Int64 _validStatus = 0;

        /// <summary>
        /// 获取或设置是否可用。(0:不可用 1:可用)
        /// </summary>
        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 ValidStatus
        {
            get { return _validStatus; }
            set { _validStatus = value; }
        }

        private string _assemblyString = string.Empty;

        /// <summary>
        /// 获取或设置程序集名称。即不包含扩展名的文件名称。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string AssemblyString
        {
            get { return _assemblyString; }
            set { _assemblyString = value; }
        }

        private string _actionGroupId = string.Empty;

        /// <summary>
        /// 获取或设置从属的命令组编号。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.String, false, false)]
        public string ActionGroupId
        {
            get { return _actionGroupId; }
            set { _actionGroupId = value; }
        }

        private Int64 _isDefaultModule = 0;

        /// <summary>
        /// 是否为默认命令。默认命令会在左键点击托盘图标后立即运行。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 IsDefaultModule
        {
            get { return _isDefaultModule; }
            set { _isDefaultModule = value; }
        }

        private Int64 _sortId = 9999;

        /// <summary>
        /// 用于排序的编号。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 SortId
        {
            get { return _sortId; }
            set { _sortId = value; }
        }

        private Int64 _isStartUpAction = 0;

        /// <summary>
        /// 获取或设置是否在主程序启动后立即运行。
        /// </summary>
        [FieldMapping(false, System.Data.DbType.Int64, false, false)]
        public Int64 IsStartUpAction
        {
            get { return _isStartUpAction; }
            set { _isStartUpAction = value; }
        }

        #endregion

        #region Constructors

        public ActionInfo() { }

        #endregion

        #region Methods

        /// <summary>
        /// 获取所有命令信息。
        /// </summary>
        /// <returns></returns>
        public static List<ActionInfo> GetAllActionsInfo()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from ActionInfo where ValidStatus = 1");

            return DbObjectMappingHelper.QueryObjectList<ActionInfo>(sql.ToString());
        }

        /// <summary>
        /// 获取指定命令组的所有命令信息。
        /// </summary>
        /// <param name="actionGroupId">命令组编码</param>
        /// <returns></returns>
        public static List<ActionInfo> GetActionsInfo(string actionGroupId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from ActionInfo where ValidStatus = 1");
            sql.AppendLine("and ActionGroupId = @id");
            sql.AppendLine("order by SortId");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return DbObjectMappingHelper.QueryObjectList<ActionInfo>(sql.ToString(),
                das.CreateParameter("@id", System.Data.DbType.String, actionGroupId));
        }

        /// <summary>
        /// 获取默认命令信息。
        /// </summary>
        /// <returns></returns>
        public static ActionInfo GetDefaultAction()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from ActionInfo where ValidStatus = 1");
            sql.AppendLine("and IsDefaultModule = 1");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return DbObjectMappingHelper.Query<ActionInfo>(sql.ToString());
        }

        /// <summary>
        /// 获取所有在主程序启动后需立即运行的命令信息。
        /// </summary>
        /// <returns></returns>
        public static List<ActionInfo> GetStartupActions()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("select * from ActionInfo where ValidStatus = 1");
            sql.AppendLine("and IsStartUpAction = 1");
            sql.AppendLine("order by SortId");

            DataAccessService das = Utils.GetDataAccessServiceInstance();
            return DbObjectMappingHelper.QueryObjectList<ActionInfo>(sql.ToString());
        }

        #endregion
    }
}
